package com.soen6441.generics;
import java.lang.reflect.Array;
import java.util.*;

class CollectionsBounds {
    static class A {};
    static class B extends A {};
    static class C extends B {};

    public static void main(String[] args) {
        ArrayList<Integer> ints = new ArrayList<Integer>();
        ints.addAll(Arrays.asList(7,31,37,42));
        ArrayList<Double> doubles = new ArrayList<Double>();
        doubles.addAll(Arrays.asList(3.14, 4.0, 4.33));

        // covariant container
        // so a collection of Integer
        // is a collection of numbers
        // think upper bound
        // anything that is at least this general
        Collection<? extends Number> numbers = ints;
        // I can only get values out of container
        for(Number number : numbers){
            System.out.println(number);
        }

        numbers = doubles;
        // won't compile not safe
        // to add double when could be ints
        //numbers.add(3.66);

        // we can also have contra variant containers
        // think lower bound
        // anything that is at most this specific
        List<? super B> bs = new ArrayList<A>();
        bs.add(new B());
        // can't put an A in here since
        // I might be pointing to a collection of B's
        //bs.add(new A());

        // I can't get anything out of the list
        // since I am not sure that I have B's or A's
        // or something else that someone else declared inherits
        // from A
        //A a = bs.get(0); // doesn't compile
        // B b = bs.get(0); // doesn't compile
        Object obj = bs.get(0); // compiles fine
    }
}
